package com.shang.base.controller;

import com.shang.base.entity.BaseResult;
import com.shang.base.service.BaseService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author: shangjp
 * @Email: shangjp@163.com
 * @Date: 2020/5/29 13:19
 * @Description:
 */
@RequestMapping
public class BaseController<T> {


    @Resource
    private BaseService<T> baseService;

    /**
     * 根据主键删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/${id}")
    public BaseResult deleteById(@PathVariable Integer id) {

        boolean delete = baseService.deleteById(id);
        if (delete){
            return new BaseResult(200,"删除成功");
        }else {
            return new BaseResult(200,"删除失败");
        }
    }

    /**
     * 添加
     *
     * @param entity
     * @return
     */
    @PostMapping
    public BaseResult insert(T entity) {
        boolean delete = baseService.insert(entity);
        if (delete){
            return new BaseResult(200,"新增成功");
        }else {
            return new BaseResult(200,"新增失败");
        }
    }

    /**
     * 根据主键搜索
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/selectById/${id}")
    public BaseResult selectById(@PathVariable Integer id) {
        boolean delete = baseService.deleteById(id);
        if (delete){
            return new BaseResult(200,"查询成功");
        }else {
            return new BaseResult(200,"查询失败");
        }
    }

    /**
     * 根据主键修改
     *
     * @param entity
     * @return
     */
    @PutMapping
    public BaseResult updateById(T entity) {
        boolean delete = baseService.updateById(entity);
        if (delete){
            return new BaseResult(200,"更新成功");
        }else {
            return new BaseResult(200,"更新失败");
        }
    }
}
